import {
	defineStore
} from 'pinia'

export const useHotelStore = defineStore('hotel', {
	state: () => ({
		_cityId: 0,
		_cityName: "",
		_businessDistrict: "",
		_checkInDate: "",
		_checkOutDate: "",
		_hotelName: "",
		_roomCount: 0,
		_hotelInfo: {},
		_orderInfo: {},
		_ratePlan: {}
	}),
	getters: {
		cityId: (state) => state._cityId,
		cityName: (state) => state._cityName,
		checkInDate: (state) => state._checkInDate,
		checkOutDate: (state) => state._checkOutDate,
		hotelName: (state) => state._hotelName,
		roomCount: (state) => state._roomCount,
		hotelInfo: (state) => state._hotelInfo,
		orderInfo: (state) => state._orderInfo,
		ratePlan: (state) =>  state._ratePlan,
		nights: (state) => {
			// 获取两个日期对象的时间戳
			let time1 = new Date(state._checkInDate).getTime();
			let time2 = new Date(state._checkOutDate).getTime();
			
			// 计算相差的毫秒数，并将其转换为天数
			let days = Math.floor((time2 - time1) / (1000 * 60 * 60 * 24));
			
			return days;
		}
	},
	actions: {
		reset() {
			
		},
		setSearchParams(payload) {
			this._cityId = payload.cityId
			this._cityName = payload.cityName
			this._checkInDate = payload.checkInDate
			this._checkOutDate = payload.checkOutDate
			this._hotelName = payload.hotelName

			// console.log(payload)
		},
		updateHotelInfo(newHotelInfo) {
			this._hotelInfo = {
				...this._hotelInfo,
				...newHotelInfo
			}
			uni.setStorageSync('hotel_info', JSON.stringify(this._hotelInfo))
		},
		updateOrderInfo(newOrderInfo) {
			this._orderInfo = {
				...this._orderInfo,
				...newOrderInfo
			}
			uni.setStorageSync('hotel_order_info', JSON.stringify(this._orderInfo))
		},
		updateRatePlan(newRatePlan) {
			this._ratePlan = {
				...this._ratePlan,
				...newRatePlan
			}
			uni.setStorageSync('hotel_rate_plan', JSON.stringify(this._ratePlan))
		}
	}
})